/**
 * 3η ΓΕ 2013-2014 - Super Market
 * ΦΡΑΓΚΟΥΛΟΠΟΥΛΟΥ ΕΜΜΑΝΟΥΕΛΑ (69298)
 * ΧΑΛΔΑΣ ΧΑΡΑΛΑΜΠΟΣ (69305)
 * ΧΟΡΜΟΒΙΤΗΣ ΙΩΑΝΝΗΣ (69325)
 * ΧΟΥΣΤΟΥΛΑΚΗ ΕΥΑΓΓΕΛΙΑ (69327)
 */
package customerGUI;

import authority.VerifyCard;
import java.awt.Cursor;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.INFORMATION_MESSAGE;
import static javax.swing.JOptionPane.WARNING_MESSAGE;
import model.*;
import supermarket.LocalBanks;

/**
 * Popup εισαγωγής Πιστωτικής Κάρτας
 */
public class MakePurchaseDialog extends javax.swing.JDialog {
    private Customer myCustomer;
    private boolean verifyOk = false;
    /**
     * Creates new form MakePurchaseDialog
     */
    public MakePurchaseDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public boolean isVerified() {
        return verifyOk;
    }
	
    public void setCustomer(Customer c) {
        myCustomer = c;
        this.holdersName.setText(c.getOwner());
        this.cvv.setText(c.getCvv());
        this.creditCard.setText(c.getCreditCardNum());
        LocalBanks lb = new LocalBanks();
        lb.fillCombo(bankCombo);
        bankCombo.setSelectedIndex(lb.getBankdId(c.getBank()));
    }
	
    /**
    * This method is called from within the constructor to initialize the
    * form. WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        payButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        creditCardPanel = new javax.swing.JPanel();
        bankLabel = new javax.swing.JLabel();
        bankCombo = new javax.swing.JComboBox();
        creditCardLabel = new javax.swing.JLabel();
        cvvLabel = new javax.swing.JLabel();
        cvv = new javax.swing.JFormattedTextField();
        ownerLabel = new javax.swing.JLabel();
        holdersName = new javax.swing.JTextField();
        creditCard = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        payButton.setText("Πληρωμή");
        payButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                payButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Ακύρωση");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        creditCardPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "ΣΤΟΙΧΕΙΑ ΠΙΣΤΩΤΙΚΗΣ ΚΑΡΤΑΣ", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 3, 12))); // NOI18N

        bankLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        bankLabel.setText("Τράπεζα");

        bankCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        bankCombo.setSelectedIndex(-1);
        bankCombo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        creditCardLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        creditCardLabel.setText("Αριθμός Πιστωτικής Κάρτας");

        cvvLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cvvLabel.setText("CVV");

        cvv.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        cvv.setColumns(3);
        try {
            cvv.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        ownerLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ownerLabel.setText("Όνομα κατόχου");

        holdersName.setColumns(40);
        holdersName.setAutoscrolls(false);
        holdersName.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        creditCard.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        creditCard.setColumns(16);
        try {
            creditCard.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-######-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        org.jdesktop.layout.GroupLayout creditCardPanelLayout = new org.jdesktop.layout.GroupLayout(creditCardPanel);
        creditCardPanel.setLayout(creditCardPanelLayout);
        creditCardPanelLayout.setHorizontalGroup(
            creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(creditCardPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(holdersName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 379, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(creditCardPanelLayout.createSequentialGroup()
                        .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(ownerLabel)
                            .add(creditCardPanelLayout.createSequentialGroup()
                                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(bankCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 158, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(bankLabel))
                                .add(18, 18, 18)
                                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                                    .add(creditCardLabel)
                                    .add(creditCard, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 152, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .add(18, 18, 18)
                        .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                            .add(cvvLabel)
                            .add(cvv, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        creditCardPanelLayout.setVerticalGroup(
            creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(creditCardPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(bankLabel)
                    .add(creditCardLabel)
                    .add(cvvLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(creditCardPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(bankCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(creditCard, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cvv, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(ownerLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(holdersName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(payButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(cancelButton))
                    .add(creditCardPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(creditCardPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(payButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

     private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
     }//GEN-LAST:event_cancelButtonActionPerformed

     private void payButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_payButtonActionPerformed

        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 
        VerifyCard v;
        if (this.creditCard.getText().isEmpty())
            v = new VerifyCard(myCustomer);
        else {
            v = new VerifyCard(this.holdersName.getText(),
                       this.creditCard.getText(),this.cvv.getText(),
                       (String)this.bankCombo.getSelectedItem()
                       );
        }
        if (v.verify()) {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 
            verifyOk = true;
            JOptionPane.showMessageDialog(this, "Η Συναλλαγή εγκρίθηκε", "Επιτυχής Συναλλαγή", INFORMATION_MESSAGE);
            dispose();
        }
        else {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            JOptionPane.showMessageDialog(this, "Η Συναλλαγή δεν εγκρίθηκε", "Ανεπιτυχής Συναλλαγή", WARNING_MESSAGE);
            dispose();
        }
		
     }//GEN-LAST:event_payButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox bankCombo;
    private javax.swing.JLabel bankLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JFormattedTextField creditCard;
    private javax.swing.JLabel creditCardLabel;
    private javax.swing.JPanel creditCardPanel;
    private javax.swing.JFormattedTextField cvv;
    private javax.swing.JLabel cvvLabel;
    private javax.swing.JTextField holdersName;
    private javax.swing.JLabel ownerLabel;
    private javax.swing.JButton payButton;
    // End of variables declaration//GEN-END:variables
}
